import { likeQuery, saveCareer, getCareerInfoById, editCareerInfoById, deleteCareerByIds } from '@/api/career'

const state = {
    careerList: [],
    careerTotal: 0
}

const mutations = {
    
    /**
     * 保存所有岗位信息到Vuex中
     * @param {*} state 
     * @param {*} data 
     */
    SET_CAREERLIST: (state, data) => {
        state.careerList = data.records
        state.careerTotal = data.total
    },
}

const actions = {

    /**
     * 获取所有岗位信息
     * @param {*} param0 
     * @returns 
     */
    likeQuery({ commit }, data) {
        return new Promise((resolve, reject) => {
            likeQuery(data).then(
                resp => {
                    const { data } = resp.data
                    commit('SET_CAREERLIST',data)
                    resolve(resp.data)
                }).catch(
                    error => {
                        reject(error)
                    })
        })
    },
    
    /**
     * 保存岗位信息
     * @param {*} param0 
     * @param {*} data 
     * @returns 
     */
    saveCareer({ commit }, data) {
        return new Promise((resolve, reject) => {
            saveCareer(data).then(
                resp => {
                    const { data } = resp.data
                    resolve(data)
                }
            ).catch( error => reject(error) )
        })
    },

    /**
     * 根据岗位id查询岗位详细信息
     * @param {*} param0 
     * @param {*} id 
     * @returns 
     */
    getCareerInfoById({ commit }, id) {
        return new Promise((resolve, reject) => {
            getCareerInfoById(id).then( resp => {
                const { data, msg } = resp.data
                if (msg == null) resolve(data)
                else reject(msg) 
            } ) 
            .catch(error => reject(error))
        })
    },

    editCareerInfoById({ commit },data) {
        const { id } = data
        return new Promise((resolve, reject) => {
            editCareerInfoById(id, data).then(resp => {
                const { data } = resp.data
                resolve(data)
            })
            .catch(error => reject(error))
        })
    },

    deleteCareersByIds({ commit },data) {
        return new Promise((resolve, reject) => {
            deleteCareerByIds(data).then( resp => {
                const { data } = resp.data
                resolve(data)
            })
            .catch( error => reject(error) )
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}